package org.dmcs.homework.statistics.classifiers.implementations;

import java.util.List;

import org.dmcs.homework.statistics.api.IMeanCalculator;
import org.dmcs.homework.statistics.classifiers.api.IClassDistanceCalculator;
import org.dmcs.homework.statistics.implementations.VectorUtil;

import Jama.Matrix;

/**
 * A tool calculating the distance between two classes on the basis of a simple, 
 * euclidean distance between their centroids.
 * @author Zielony
 * @version 1.0
 */
public class SimpleCentroidClassDistanceCalculator implements
		IClassDistanceCalculator {
	/**
	 * A tool for calculating means.
	 */
	protected IMeanCalculator meanCalculator;
	
	/**
	 * A tool for dealing with vector operations.
	 */
	protected VectorUtil vectorUtil;
	
	/**
	 * {@inheritDoc}
	 */
	public double calculateClassDistance(List<Integer> classOne,
			List<Integer> classTwo, int[] featureSet, double[][] data) {
		/*
		 * Tablice zawierajace indeksy punktow nalezacych do klas.
		 */
		int[] classOneArray = new int[classOne.size()];
		int[] classTwoArray = new int[classTwo.size()];
		
		/*
		 * Przepisywanie z list do tablic dla kompatybilnosci.
		 */
		for(int i=0; i<classOne.size(); i++) {
			classOneArray[i] = classOne.get(i);
		}
		for(int i=0; i<classOne.size(); i++) {
			classTwoArray[i] = classTwo.get(i);
		}
		
		/*
		 * Obliczanie centroidow klas.
		 */
		double[] meanVectorOne = meanCalculator.calculateMeanVector(new Matrix(data), 
				classOneArray, featureSet);
		double[] meanVectorTwo = meanCalculator.calculateMeanVector(new Matrix(data),
				classOneArray, featureSet);
		
		/*
		 * Obliczanie wektora miedzy centroidami.
		 */
		double[] distanceVector = vectorUtil.subtract(meanVectorOne, meanVectorTwo);
		
		/*
		 * Odleglosc miedzy centroidami.
		 */
		return vectorUtil.length(distanceVector);
	}
}